﻿

namespace ExtPageBuilder
{
    using Ext.Net;
    using System.Linq;
    using System.Collections.Generic;
    public partial class ExtListPage : ExtPage
    {
        #region Properties
        private GridPanel _Grid;

        private DynamicExtControl _DyExt = new DynamicExtControl();

        public GridPanel Grid
        {
            get { return _Grid; }
            set { _Grid = value; }
        }
        #endregion Properties
        public ExtListPage()
        {
            this.PageType = ExtPageType.List;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="editable"></param>
        public ExtListPage(bool editable)
        {
            this.PageType = ExtPageType.List;
            this.Editable = editable;
        }
        protected override void PageObjectChanged()
        {
            this.BuilPageStore();
            this.BuilExtGrid();
        }
        /// <summary>
        /// 
        /// </summary>
        public override void BuilExtGrid()
        {
            this._Grid = new GridPanel();
            this._Grid.ID = "grdList";
            this._Grid.IDMode = IDMode.Static;
            this._Grid.Store.Add(this._PageStore);
            this._Grid.Layout = "FitLayout";
            this._Grid.Frame = true;
            this._Grid.BodyBorder = 0;
            this._Grid.Border = false;
            //Editing
            if (this.Editable)
            {
                CellEditing ce = new CellEditing();
                this._Grid.Plugins.Add(ce);

            }
            //Selection model
            if (this.Editable)
            {
                CellSelectionModel celSelect = new CellSelectionModel();
                this._Grid.SelectionModel.Add(celSelect);
            }
            else
            {
                RowSelectionModel rowSelect = new RowSelectionModel();
                this._Grid.SelectionModel.Add(rowSelect);
            }
            //Column
            CommandColumn _cl = new CommandColumn();
            if (!this.Editable)
            {
                //Command
                if (this.PageObject.Card_Page != null && this.PageObject.Card_Page > 0)
                {
                    string pk = string.Empty;
                    foreach (Object_property op in this.PageObject.Items)
                    {
                        if (op.PreKey != null && op.PreKey.Value == true)
                        {
                            pk = op.Name;
                            break;
                        }
                    }
                    GridCommand _grdComnd = new GridCommand();
                    ConfigItem cif = new ConfigItem("card", this.PageObject.Card_Page.Value.ToString());
                    ConfigItem cif2 = new ConfigItem("rowid", pk, ParameterMode.Value);
                    _grdComnd.CustomConfig.Add(cif);
                    _grdComnd.CustomConfig.Add(cif2);
                    _grdComnd.Icon = Icon.PageEdit;
                    _grdComnd.CommandName = "RowCommand";
                    _cl.Commands.Add(_grdComnd);
                    _cl.Listeners.Command.Fn = "onCommand";
                }

            }
            _cl.Hideable = false;
            _cl.Width = 30;
            _cl.ID = "clClearFilter";
            Button clearFilter = new Button();
            clearFilter.ID = "btnClearFilter";
            clearFilter.Icon = Icon.Decline;
            clearFilter.Listeners.Click.Fn = "clearFilter";
            _cl.HeaderItems.Add(clearFilter);
            this._Grid.ColumnModel.Columns.Add(_cl);

            foreach (Object_property op in this.PageObject.Items)
            {
                ColumnBase _c = _DyExt.Buil_grid_column(op, this.LanguageID, this.Editable);
                if (_c == null)
                    continue;
                if (this._Grid.ColumnModel.Columns.SingleOrDefault(c => c.ID == _c.ID) == null)
                {
                    Field hf = _DyExt.Buil_control(op, this.LanguageID, true, new string[] { "HDF" });
                    hf.ID += "Filter";
                    string ff = "applyFilter";
                    hf.AddListener("blur", ff);
                    _c.HeaderItems.Add(hf);
                    this._Grid.ColumnModel.Columns.Add(_c);
                }
            }
            //Paging
            PagingToolbar pagt = new PagingToolbar();
            pagt.DisplayInfo = true;
            this._Grid.BottomBar.Add(pagt);


        }
        /// <summary>
        /// 
        /// </summary>
        public void BuilPageStore()
        {
            this._PageStore = new Store();
            this._PageStore.RemoteSort = true;
            this._PageStore.RemotePaging = true;
            this._PageStore.RemoteFilter = true;
            this._PageStore.ID = "stoList";
            this._PageStore.IDMode = IDMode.Static;
            Model md = new Model();
            this._PageStore.Model.Add(md);

            foreach (Object_property op in this.PageObject.Items)
            {
                if (op.PreKey != null && op.PreKey.Value == true)
                {
                    md.IDProperty = op.Name;
                }
                ModelField _f = _DyExt.Buil_recordfield(op, this.LanguageID);
                if (_f == null)
                    continue;
                if (this._PageStore.Model[0].Fields.SingleOrDefault(f => f.Name == _f.Name) == null)
                    this._PageStore.Model[0].Fields.Add(_f);
            }
            AjaxProxy ajp = new AjaxProxy();
            ajp.Url = "Services/RemoteData.ashx";
            JsonReader jsonr = new JsonReader();
            jsonr.Root = "data";
            jsonr.TotalProperty = "total";
            jsonr.SuccessProperty = "success";
            jsonr.MessageProperty = "message";
            ajp.Reader.Add(jsonr);

            JsonWriter jsonw = new JsonWriter();
            jsonw.Root = "data";
            jsonw.Encode = true;
            jsonw.WriteAllFields = false;
            ajp.Writer.Add(jsonw);

            this._PageStore.Proxy.Add(ajp);

            StoreParameter sp = new StoreParameter("action", "operation.action", ParameterMode.Raw);
            this._PageStore.SyncParameters.Add(sp);
            StoreParameter sp2 = new StoreParameter("ob", this._PageObject.ID.ToString(), ParameterMode.Value);
            this._PageStore.SyncParameters.Add(sp2);

            Parameter pr = new Parameter("filter", "", ParameterMode.Value);
            this._PageStore.AutoLoadParams.Add(pr);

            Parameter pr2 = new Parameter("ob", this._PageObject.ID.ToString(), ParameterMode.Value);
            this._PageStore.AutoLoadParams.Add(pr2);
        }
    }
}
